package com.wceManage.manage.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang.time.DateUtils;


public class StringUtil {
	public static final String DATE_FORMAT_DEFAULT;
	public static final String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_DATETIME_MINUTE = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT_DATE = "yyyy-MM-dd";
	public static final String DATE_FORMAT_YEAR = "yyyy";
	public static final String DATE_FORMAT_MONTH = "yyyy-MM";
	private static SimpleDateFormat dateFormat;
	
	static {
		DATE_FORMAT_DEFAULT = DATE_FORMAT_DATE;
		dateFormat = new SimpleDateFormat(DATE_FORMAT_DEFAULT);
	}
	
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}
	
	public static String join(String[] array, String sep) {
		if (array == null) {
			return null;
		}
		if (array.length == 0) {
			return "";
		}
		if (sep == null) {
			sep = "";
		}
		// 预处理，得到长度。
		int capacity = 0;
		for (int i = 0; i < array.length; i++) {
			array[i] = String.valueOf(array[i]);
			capacity = capacity + array[i].length() + sep.length();
		}
		char[] result = new char[capacity - sep.length()];
		int begin = 0;
		for (int i = 0; i < array.length; i++) {
			array[i].getChars(0, array[i].length(), result, begin);
			begin = begin + array[i].length();
			if (begin == result.length) {
				break;
			}
			sep.getChars(0, sep.length(), result, begin);
			begin = begin + sep.length();
		}
		return new String(result);
	}

	public static String formatTimestamp(Timestamp ts) {
		try {
			return dateFormat.format(ts);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("格式化date抛出异常");
			return null;
		}
	}

	public static String formatTimestamp(Timestamp ts, String format) {
		try {
			return new SimpleDateFormat(format).format(ts);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("格式化date抛出异常");
			return null;
		}
	}

	public static Timestamp parseTimestamp(String value) {
		try {
			return new Timestamp(dateFormat.parse(value).getTime());
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("转换date抛出异常");
			return null;
		}
	}

	public static Timestamp parseTimestamp(String value, String format) {
		try {
			return new Timestamp(new SimpleDateFormat(format).parse(value).getTime());
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("按照格式转换date抛出异常");
			return null;
		}
	}

	public static String formatDate(Date date) {
		try {
			return dateFormat.format(date);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("格式化date抛出异常");
			return null;
		}
	}
	
	public static String formatDate(Date date, String format) {
		try {
			return new SimpleDateFormat(format).format(date);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("格式化date抛出异常");
			return null;
		}
	}

	public static String formatDateToStr(String date, String format) {
		try {
			return new SimpleDateFormat(format).format(date);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("格式化date抛出异常");
			return null;
		}
	}
	
	public static Date parseDate(String value) {
		try {
			return dateFormat.parse(value);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("转换date抛出异常");
			return null;
		}
	}

	public static Date parseDate(String value, String format) {
		try {
			return new SimpleDateFormat(format).parse(value);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("按照格式转换date抛出异常");
			return null;
		}
	}

	public static Integer parseInteger(String value) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("转换int抛出异常");
			return null;
		}
	}

	public static Integer[] parseIntegerArray(String[] values) {
		try {
			Integer[] result = new Integer[values.length];
			for (int i = 0; i < values.length; i++) {
				result[i] = Integer.parseInt(values[i]);
			}
			return result;
		} catch (Exception e) {
			// System.out.println("转换Long抛出异常");
			return null;

		}
	}

	public static Double parseDouble(String value) {
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {
			// System.out.println("转换Double抛出异常");
			return null;

		}
	}
	
	public static Long parseLong(String value) {
		try {
			return Long.parseLong(value);
		} catch (Exception e) {
			// System.out.println("转换Long抛出异常");
			return null;

		}
	}

	public static Long[] parseLongArray(String[] values) {
		try {
			Long[] result = new Long[values.length];
			for (int i = 0; i < values.length; i++) {
				result[i] = Long.parseLong(values[i]);
			}
			return result;
		} catch (Exception e) {
			// System.out.println("转换Long抛出异常");
			return null;

		}
	}
	
	public static String transform(String content) {
		if (content == null)
			return null;
		content = content.replaceAll("&", "&amp;");
		content = content.replaceAll("<", "&lt;");
		content = content.replaceAll(" ", "&nbsp;");
		content = content.replaceAll(">", "&gt;");
		content = content.replaceAll("\n", "<br>");
		// content = content.replaceAll("\r\n", "<br>");
		return content;
	}
	
	
	
	public static String toString(Object o) {
		return (o == null) ? "" : o.toString();
	}

	public static boolean isValidIp(String ip) {
		String pattern = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
		return ip.matches(pattern);
	}

	public static String getRandomPwd(int length) {
		StringBuilder pwd = new StringBuilder();
		String number = "0123456789";
		while (pwd.length() < length) {
			pwd.append(number.charAt((int) (Math.random() * number.length())));
		}
		return pwd.toString();
	}
	
	/**
     * @参数： @param obj
     * @参数： @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(Object obj) {
        if (null == obj) {
            return true;
        }
        if (obj instanceof String) {
            return ((String) obj).trim().isEmpty();
        }
        return !(obj instanceof Number) ? false : false;
    }
    
    /**
     * 日期调整
     * @param date
     * @param type 大写M:月,d:日,h:小时,小写m:分
     * @param time
     * @return
     */
    public static Date changeTime(Date date, String type, int time){
    	if(type.equals("M")){
    		return DateUtils.addMonths(date, time);
    	}else if(type.equals("d")){
    		return DateUtils.addDays(date, time);
    	}else if(type.equals("h")){
    		return DateUtils.addHours(date, time);
    	}else if(type.equals("m")){
    		return DateUtils.addMinutes(date, time);
    	}
		return null;
    }
    
    /**
	 * 把日期字符串转化成日期对象
	 * 
	 * Title:
	 * 
	 * @param strValue
	 *            日期字符串 例如:"2007-1-18" or "2007-1-18 11:11:11"
	 * @param strToPattern
	 *            需要转换成的格式 默认 yyyy-MM-dd HH:mm:ss
	 * @return 日期:2013-7-29
	 */

	public static Date strToDate(String strValue, String strToPattern) {
		if (strToPattern == null)
			strToPattern = "yyyy-MM-dd HH:mm:ss";
		if (strValue != null) {
			try {
				SimpleDateFormat objFormatter = new SimpleDateFormat(strToPattern);
				return objFormatter.parse(strValue);
			} catch (Exception e) {
				// logger.error(e);
				return null;
			}
		}
		return null;
	}
	
	/**
	 * 把日期字符串转化成日期对象
	 * 
	 * Title:
	 * 
	 * @param strValue
	 *            日期字符串 yyyy-MM-dd HH:mm:ss
	 * @return 日期:2013-10-15
	 */

	public static Date strToDate(String strValue) {
		if (strValue != null) {
			try {
				SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return objFormatter.parse(strValue);
			} catch (Exception e) {
				// logger.error(e);
				return null;
			}
		}
		return null;
	}
}
